﻿<?php
	require_once("mysql_help.class.php");
	require_once("../config/admin_config.php");
	
	class Job
	{
		var $sqlHelper;
		
		public function __construct()
		{
			$this->sqlHelper = new db_Mysql();
		}
		
		
		/*功能：计算招聘信息页面的总页数
		//$place工作地点  如果等于0，代表所有城市
		//$industry(int)  行业  如果等于0，代表所有行业
		//$payment(int)  薪酬范围  如果等于0，代表无特殊薪酬要求
		//$job_attribute(int)  工作属性（全职[1]、兼职[2]、实习[3]）  如果等于0，代表所有类型
		*/
		public function GetJobPageNum($place,$industry,$payment,$job_attribute)
		{
			$place_choose = $industry_choose = $payment_choose = $job_attribute_choose = null;//数据库中各个数据的搜索条件
			
			//工作地点
			if($place == 0) $place_choose = "";
			else $place_choose = " and place = '".$place."' ";
			
			//行业
			if($industry == 0) $industry_choose = "";
			else $industry_choose = " and industry = '".$industry."' ";
			//echo '<script>alert("'.$industry_choose.'");</script>';
			
			//薪酬范围
			if($payment == 0) $payment_choose = "";//无要求
			else if($payment == 1)//工资低于2000
			{
				$payment_choose = " and payment < 2000 ";
			}
			else if($payment > 4)//工资>=1W
			{
				$payment_choose = " and payment > 8000 ";
			}
			else//薪水要求在2000到10000之间的
			{
				$payment_real_down = 2000 * ($payment - 1);//工资下限
				$payment_real_top = 2000 * $payment;//工资上限
				$payment_choose = " and payment > '".$payment_real_down."' and payment <= '".$payment_real_top."' ";
			}
			
			//工作属性
			if($job_attribute == 0) $job_attribute_choose = "";//无要求
			else $job_attribute_choose = " and job_attribute = '".$job_attribute."' ";
			
			//执行sql操作
			global $Page_Size;
			$cmd = " select count(id) as amount from employ_message where 1=1 ".$place_choose.$industry_choose.$payment_choose.$job_attribute_choose;
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$row = $this->sqlHelper->fetch_array($result);
			$num_page = $row["amount"];//获取总记录数
			$num_page = (int)(($num_page + $Page_Size - 1)/$Page_Size);//获取总页数
			$this->sqlHelper->dbclose();
			return $num_page;
		}
		
		
		
		/*功能：按照条件查找招聘信息
		//$place工作地点  如果等于0，代表所有城市
		//$industry(int)  行业  如果等于0，代表所有行业
		//$payment(int)  薪酬范围  如果等于0，代表无特殊薪酬要求
		//$job_attribute(int)  工作属性（全职[1]、兼职[2]、实习[3]）  如果等于0，代表所有类型
		//$num_page  分页数
		*/
		public function FindJobByClassify($place,$industry,$payment,$job_attribute,$num_page)
		{
			$place_choose = $industry_choose = $payment_choose = $job_attribute_choose = null;//数据库中各个数据的搜索条件
			
			//工作地点
			if($place == 0) $place_choose = "";
			else $place_choose = " and place = '".$place."' ";
			
			//行业
			if($industry == 0) $industry_choose = "";
			else $industry_choose = " and industry = '".$industry."' ";
			
			
			//薪酬范围
			if($payment == 0) $payment_choose = "";//无要求
			else if($payment == 1)//工资低于2000
			{
				$payment_choose = " and payment < 2000 ";
			}
			else if($payment > 4)//工资>=1W
			{
				$payment_choose = " and payment > 8000 ";
			}
			else//薪水要求在2000到10000之间的
			{
				$payment_real_down = 2000 * ($payment - 1);//工资下限
				$payment_real_top = 2000 * $payment;//工资上限
				$payment_choose = " and payment >'".$payment_real_down."' and payment <= '".$payment_real_top."' ";
			}
			//echo '<script>alert("'.$payment_choose.'");</script>';
			
			//工作属性
			if($job_attribute == 0) $job_attribute_choose = "";//无要求
			else $job_attribute_choose = " and job_attribute = '".$job_attribute."' ";
			
			//执行sql操作
			global $Page_Size;
			//echo '<script>alert("'.$place_choose.'");</script>';
			//echo '<script>alert("'.$industry_choose.'");</script>';
			//echo '<script>alert("'.$payment_choose.'");</script>';
			//echo '<script>alert("'.$job_attribute_choose.'");</script>';
			$cmd = 
				"SELECT employ_company_city. * , industry.name AS name_industry
				FROM (

					SELECT employ_company. * , city.name AS name_city
					FROM (

						SELECT employ_message. * , company.name AS name_company
						FROM employ_message
						LEFT JOIN company ON employ_message.company_id = company.id
						where 1=1 ".$place_choose.$industry_choose.$payment_choose.$job_attribute_choose.
					") AS employ_company
					LEFT JOIN city ON employ_company.place = city.id
				) AS employ_company_city
				LEFT JOIN industry ON employ_company_city.industry = industry.id
				LIMIT ".($num_page-1)*$Page_Size.",".$Page_Size;//按指定要求，获取分页
			$this->sqlHelper->dbconnect();
			$resulte = $this->sqlHelper->execute($cmd);//返回搜索信息   need to free after use
			$this->sqlHelper->dbclose();
			return $resulte;
		}
		
		/*功能：按照条件查找招聘信息并输出html布局
		//$place工作地点  如果等于0，代表所有城市
		//$industry(int)  行业  如果等于0，代表所有行业
		//$payment(int)  薪酬范围  如果等于0，代表无特殊薪酬要求
		//$job_attribute(int)  工作属性（全职[1]、兼职[2]、实习[3]）  如果等于0，代表所有类型
		//$num_page  页码
		*/
		public function JobLayout($id_graduate,$place,$industry,$payment,$job_attribute,$num_page)
		{
			$result = $this->FindJobByClassify($place,$industry,$payment,$job_attribute,$num_page);
			while($row = $this->sqlHelper->fetch_array($result))
			{
				echo'
					<div class="job_listview_box">
						<div class="job_company_logo"><a href="../company/Recruitment_list.php?companyId='.$row["id"].'">
							<img src="../data/company_logo/13266194330001519mm.png" width="60" height="60" border="0" /></a></div>
						<div class="job_listview_title">
							<a href="Recruitment_detail.php?id_employMsg='.$row["id"].'&graduateId='.$id_graduate.'">'.$row["title"].'</a>
						</div>
						<div class="job_listview_brifintro">'.$row["name_company"].' | '.$row["payment"],'/月  <span class="company_aera">'.$row["name_city"].'</span></div>
						<div class="job_listview_brifintro"><a href="../company/Recruitment_list.php?companyId='.$row["id"].'">'.$row["name_company"].'</a> 发布</div>
					</div>';
			}
			$this->sqlHelper->free_result($result);	
		}
		
		/*功能：显示底下分页
		@$num_page(int) 请求的页面
		@$place(int)  工作地点  如果等于0，代表所有城市
		@$industry(int)  行业  如果等于0，代表所有行业
		@$payment(int)  薪酬范围  如果等于0，代表无特殊薪酬要求
		@$job_attribute(int)  工作属性（全职[1]、兼职[2]、实习[3]）  如果等于0，代表所有类型
		@$addr_page(varchar)  页面地址。如(../../jobList.php?page=)
		*/
		public function Show_PageList($id_graduate,$num_page,$place,$industry,$payment,$job_attribute,$addr_page)
		{
			$num_page_totle = $this->GetJobPageNum($place,$industry,$payment,$job_attribute);//得到总页数
			if($num_page_totle < 1)
			{
				echo "无记录哦亲！";//总页数为0
				return;
			}
			if($num_page < 1 || $num_page > $num_page_totle) $num_page = 1;//如果请求的页面不在范围内，在现实第一页
			//写好“首页、上一页”
			$str = "";
			if($num_page == 1)	$str .= '<div class="page_box">首页</div> <div class="page_box">上一页</div> ';
			else $str .= '<div class="page_box" onclick="location.href=\''.$addr_page.'1&graduateId='.$id_graduate.'&place='.
				$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">首页</div> '.
				'<div class="page_box" onclick="location.href=\''.$addr_page.($num_page - 1).'&graduateId='.$id_graduate.'&place='.
				$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">上一页</div>';
				//[<a href = '".$addr_page."1'>first</a>] [<a href = '".$addr_page.($num_page - 1)."'>pre</a>] ";
			//写好中间显示的页码数
			if($num_page < 10)
			{
				//echo '<script>alert("'.$num_page_totle.'");</script>';
				for($i = 1;$i < 12 && $i <= $num_page_totle;$i++)
				{
					if($i == $num_page) $str .= '<div class="page_box on"  onclick="location.href=\''.$addr_page.$i.'&graduateId='.$id_graduate.'&place='.
						$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">'.$i.'</div>';
					else $str .= '<div class="page_box"  onclick="location.href=\''.$addr_page.$i.'&graduateId='.$id_graduate.'&place='.
						$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">'.$i.'</div>';
				}
			}
			else
			{
				$num_page_top = $num_page + 5;//显示页数的上限
				for($i = $num_page - 5;$i < $num_page_top && $i <= $num_page_totle;$i++)
				{
					if($i == $num_page) $str .= '<div class="page_box on"  onclick="location.href=\''.$addr_page.$i.'&graduateId='.$id_graduate.'&place='.
						$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">'.$i.'</div>';
					else $str .= '<div class="page_box"  onclick="location.href=\''.$addr_page.$i.'&graduateId='.$id_graduate.'&place='.
						$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">'.$i.'</div>';
				}
			}
			//写好“尾页、下一页”
			if($num_page == $num_page_totle) $str .= '<div class="page_box">下一页</div> <div class="page_box">尾页</div> ';
			else $str .= '<div class="page_box" onclick="location.href=\''.$addr_page.($num_page + 1).'&graduateId='.$id_graduate.'&place='.
				$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">下一页</div>'.
				'<div class="page_box" onclick="location.href=\''.$addr_page.$num_page_totle.'&graduateId='.$id_graduate.'&place='.
				$place.'&industry='.$industry.'&payment='.$payment.'&full_time='.$job_attribute.'\';">尾页</div> ';
					
			//"[<a href = '".$addr_page.($num_page + 1)."'>next</a>] [<a href = '".$addr_page.$num_page_totle."'>last</a>]";
			//输出
			echo $str;
		}
			
		
		
		
		/*功能：按照公司来找工作
		//$companyId：所要查找的公司的id
		*/
		public function FindJobByCompany($companyId)
		{
			$cmd = "select * from employ_message where company_id = '".$companyId."'";
			$this->sqlHelper->dbconnect();
			$result = $this->sqlHelper->execute($cmd);
			$this->sqlHelper->dbclose();
			return $result;
		}
	}
?>